package cn.csxw.openchina.ui.activity;

import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseActivity;
import cn.csxw.openchina.dagger2.component.DaggerBlogDetailActivityComponent;
import cn.csxw.openchina.dagger2.module.BlogDetailActivityModdule;
import cn.csxw.openchina.dagger2.presenter.BlogDetailActivityPresenter;
import cn.csxw.openchina.entity.bean.BlogBean;

/**
 * Created by hp on 2017/1/15.
 */

public class BlogDetailActivity extends BaseActivity {

    long exitTime = System.currentTimeMillis();
    public BlogBean mBlogBean;
    public BlogBean mRecommdBean;
    @BindView(R.id.tv_blog_detail_title)
    TextView mTvBlogDetailTitle;
    @BindView(R.id.tv_blog_detail_time)
    TextView mTvBlogDetailTime;
    @BindView(R.id.tv_blog_detail_author)
    TextView mTvBlogDetailAuthor;
    @BindView(R.id.wv_blog_detail)
    WebView mWvBlogDetail;
    @BindView(R.id.iv_blog_opt)
    ImageView mIvBlogOpt;
    @BindView(R.id.iv_blog_message)
    ImageView mIvBlogMessage;
    @BindView(R.id.tv_blog_message_count)
    TextView mTvBlogMessageCount;
    @BindView(R.id.iv_blog_write)
    ImageView mIvBlogWrite;
    @BindView(R.id.iv_blog_favor)
    ImageView mIvBlogFavor;
    @BindView(R.id.iv_blog_repost)
    ImageView mIvBlogRepost;
    @BindView(R.id.iv_blog_report)
    ImageView mIvBlogReport;
    @BindView(R.id.pb_blog_detail)
    ProgressBar mPbBlogDetail;
    @BindView(R.id.iv_blog_write_content)
    ImageButton mIvBlogWriteContent;
    @BindView(R.id.iv_bq)
    ImageView mIvBq;
    @BindView(R.id.iv_send)
    ImageView mIvSend;
    @BindView(R.id.ll_blog_write)
    LinearLayout mLlBlogWrite;
    @BindView(R.id.ll_blog_share)
    LinearLayout mLlBlogShare;
    @Inject
    BlogDetailActivityPresenter mBlogDetailActivityPresenter;


    @Override
    public int getLayoutRes() {
        return R.layout.activity_blog_detail;
    }

    @Override
    public void initView() {
        mActionBar.setTitle("博客详情");
        WebSettings webSettings = mWvBlogDetail.getSettings();
        webSettings.setJavaScriptEnabled(true);//支持JS
        webSettings.setAllowFileAccess(true);//允许WebView访问文件数据  
        webSettings.setBuiltInZoomControls(true);//支持内容缩放控制 
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        //TODO 根据对面传过来的Bean类型来接收
        mBlogBean = (BlogBean) getIntent().getSerializableExtra("blog");
        mRecommdBean = (BlogBean) getIntent().getSerializableExtra("recommend");
        getNewsData();
    }

    private void getNewsData() {
        DaggerBlogDetailActivityComponent.builder()
                .blogDetailActivityModdule(new BlogDetailActivityModdule(this))
                .build()
                .inject(this);
        String blogId = "";
        String recommendId = "";
        if (mBlogBean != null) {
            blogId = mBlogBean.id;
            mBlogDetailActivityPresenter.callNetWork(blogId);
        }
        if (mRecommdBean != null) {
            recommendId = mRecommdBean.id;
            mBlogDetailActivityPresenter.callNetWork(recommendId);
        }
    }


    @Override
    public void onClick(View v, int id) {

    }

    @OnClick({R.id.iv_blog_write_content,R.id.iv_blog_opt, R.id.iv_blog_message, R.id.iv_blog_write, R.id.iv_blog_favor, R.id.iv_blog_repost, R.id.iv_blog_report})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_blog_message:
                break;
            case R.id.iv_blog_opt:
            case R.id.iv_blog_write:
                startShareAnima();
                break;
            case R.id.iv_blog_favor:
                break;
            case R.id.iv_blog_repost:
                break;
            case R.id.iv_blog_report:
                break;
            case R.id.iv_blog_write_content:
                startWriteAnima();
                break;
        }
    }

    private void startShareAnima() {
        TranslateAnimation taShare = new TranslateAnimation(0, 0, 0, mLlBlogShare.getHeight());
        taShare.setDuration(200);
        mLlBlogShare.startAnimation(taShare);
        taShare.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLlBlogWrite.setVisibility(View.VISIBLE);
                mLlBlogShare.setVisibility(View.GONE);
                TranslateAnimation taWrite = new TranslateAnimation(0, 0, mLlBlogShare.getHeight(),0);
                taWrite.setDuration(200);
                mLlBlogWrite.startAnimation(taWrite);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void startWriteAnima() {

        TranslateAnimation taWrite = new TranslateAnimation(0, 0,0, mLlBlogShare.getHeight());
        taWrite.setDuration(200);
        mLlBlogWrite.startAnimation(taWrite);

        taWrite.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLlBlogShare.setVisibility(View.VISIBLE);
                mLlBlogWrite.setVisibility(View.GONE);
                TranslateAnimation taShare = new TranslateAnimation(0, 0, mLlBlogShare.getHeight(), 0);
                taShare.setDuration(200);
                mLlBlogShare.startAnimation(taShare);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }


    public void doNetWork(BlogBean blog) {
        if (blog == null) {
            return;
        }
        mWvBlogDetail.loadUrl(blog.url);
        mTvBlogDetailTitle.setText(blog.title);
        mTvBlogDetailTime.setText(blog.pubDate);
        mTvBlogDetailAuthor.setText(blog.author);
        mWvBlogDetail.setWebChromeClient(mWebChromeClient);
    }

    WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if(mPbBlogDetail!=null){
                if (newProgress > 20) {
                    mPbBlogDetail.setVisibility(View.GONE);
                } else if (newProgress == 0) {
                    mPbBlogDetail.setVisibility(View.VISIBLE);
                } else {
                    mPbBlogDetail.setVisibility(View.VISIBLE);
                }
            }

        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWvBlogDetail.canGoBack()) {
            mWvBlogDetail.goBack();
            return true;
        } else {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出WebView", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
        }
        return true;
    }
}
